package com.oracle.lingshangejia.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.lws.basic.utils.common.AdapterIntegerDefault0;
import com.lws.basic.utils.common.DoubleDefault0Adapter;
import com.oracle.lingshangejia.Constant;
import com.oracle.lingshangejia.R;
import com.oracle.lingshangejia.activity.special.ChanHou42DetailActivity;
import com.oracle.lingshangejia.activity.special.ChanHouDetailActivity;
import com.oracle.lingshangejia.activity.special.ChanJianDetailActivity;
import com.oracle.lingshangejia.activity.special.Chanjian25DetailActivity;
import com.oracle.lingshangejia.activity.special.ErTongZongYiDetailActivity;
import com.oracle.lingshangejia.activity.special.GXYDetailActivity;
import com.oracle.lingshangejia.activity.special.HealthCheckDetailActivity;
import com.oracle.lingshangejia.activity.special.LaoNianRenDetailActivity;
import com.oracle.lingshangejia.activity.special.TnbDetailActivity;
import com.oracle.lingshangejia.activity.special.Xer12DetailActivity;
import com.oracle.lingshangejia.activity.special.Xer18DetailActivity;
import com.oracle.lingshangejia.activity.special.Xer36DetailActivity;
import com.oracle.lingshangejia.activity.special.XerDetailActivity;
import com.oracle.lingshangejia.adapter.SpecialLsitAdapter;
import com.oracle.lingshangejia.bean.SpecialListBean;
import com.oracle.lingshangejia.bean.User;
import com.oracle.lingshangejia.model.OkgoNetwork;
import com.oracle.lingshangejia.mvp.base.BaseActivity;
import com.oracle.lingshangejia.utils.MyLogger;
import com.oracle.lingshangejia.utils.MyToast;
import com.oracle.lingshangejia.utils.ObserverOnNext;
import com.oracle.lingshangejia.utils.RxUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
//居民专项列表
public class SpecialListActivity extends BaseActivity implements BaseQuickAdapter.OnItemClickListener {

    @BindView(R.id.rv)
    RecyclerView mRv;
    @BindView(R.id.srl)
    SmartRefreshLayout mSrl;
    private SpecialLsitAdapter mAdapter;
    private int mId;
    private int mOffset = 0;
    private int mLimit = 10;

    @Override
    public int setLayoutId() {
        return R.layout.activity_special_list;
    }


    @Override
    public void initData() {
        super.initData();
        ButterKnife.bind(this);
        mId = getIntent().getIntExtra("jmid", 0);
        initRecycleview();
        initRefresh();
        getData();
    }

    private void initRecycleview() {
        mRv.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new SpecialLsitAdapter(null);
        mAdapter.setOnItemClickListener(this);
        mRv.setAdapter(mAdapter);

    }

    private void loadmore() {
        showLoading();
        mOffset += 10;
        Observable.create(new ObservableOnSubscribe<SpecialListBean>() {
            @Override
            public void subscribe(ObservableEmitter<SpecialListBean> emitter) throws Exception {
                Map<Object, Object> map = new HashMap<>();
                map.put("token", User.getToken());
                map.put("mark", 1);
                map.put("offset", mOffset);
                map.put("limit", mLimit);
                map.put("jmId", mId);
                JSONObject jsonObject = new JSONObject(map);
                MyLogger.i(jsonObject.toString());
                Gson gson = new GsonBuilder()
                        .registerTypeAdapter(Integer.class, new AdapterIntegerDefault0())
                        .registerTypeAdapter(int.class, new AdapterIntegerDefault0())
                        .registerTypeAdapter(Double.class, new DoubleDefault0Adapter())
                        .registerTypeAdapter(double.class, new DoubleDefault0Adapter())
                        .create();
                String json = OkgoNetwork.post(Constant.special_lsit, jsonObject.toString());
                MyLogger.i(json);
                emitter.onNext(gson.fromJson(json, SpecialListBean.class));

            }
        }).compose(RxUtils.io_main()).subscribe(new ObserverOnNext<SpecialListBean>() {
            @Override
            public void onNext(SpecialListBean bean) {
                if (bean.getMsg().equals("Token已过期")) {
                    startActivity(new Intent(SpecialListActivity.this, LoginActivity.class));

                } else {
                    if (bean.getData() != null && bean.getData().getRows().size() > 0) {
                        mAdapter.addData(bean.getData().getRows());
                    } else {
                        View emptyView = getLayoutInflater().inflate(R.layout.view_empty, null);
                        mAdapter.setEmptyView(emptyView);
                        MyToast.show("没有数据了");
                    }
                }
                mSrl.finishLoadmore();
                dismissLoading();
            }
        });
    }

    private void getData(){
        showLoading();
        Observable.create(new ObservableOnSubscribe<SpecialListBean>() {
            @Override
            public void subscribe(ObservableEmitter<SpecialListBean> emitter) throws Exception {
                Map<Object, Object> map = new HashMap<>();
                map.put("jmId", mId);
                map.put("token", User.getToken());
                map.put("mark", 1);
                map.put("offset", mOffset);
                map.put("limit", mLimit);
                JSONObject jsonObject = new JSONObject(map);
                MyLogger.i(jsonObject.toString());
                Gson gson = new GsonBuilder()
                        .registerTypeAdapter(Integer.class, new AdapterIntegerDefault0())
                        .registerTypeAdapter(int.class, new AdapterIntegerDefault0())
                        .registerTypeAdapter(Double.class, new DoubleDefault0Adapter())
                        .registerTypeAdapter(double.class, new DoubleDefault0Adapter())
                        .create();
                String json = OkgoNetwork.post(Constant.special_lsit, jsonObject.toString());
                MyLogger.i(json);
                emitter.onNext(gson.fromJson(json, SpecialListBean.class));

            }
        }).compose(RxUtils.io_main()).subscribe(new ObserverOnNext<SpecialListBean>() {
            @Override
            public void onNext(SpecialListBean bean) {
                dismissLoading();
                if (bean.getMsg().equals("Token已过期")) {
                    startActivity(new Intent(SpecialListActivity.this, LoginActivity.class));

                } else {
                    if (bean.getData() != null && bean.getData().getRows().size() > 0) {
                           mAdapter.setNewData(bean.getData().getRows());
                    } else {
                        View emptyView = getLayoutInflater().inflate(R.layout.view_empty, null);
                        mAdapter.setEmptyView(emptyView);
                        MyToast.show("没有数据了");
                    }
                }
                mSrl.finishRefresh();

            }
        });

    }

    private void initRefresh() {
        mSrl.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                getData();
            }
        });
        mSrl.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                loadmore();
            }
        });
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        Intent intent = new Intent();
        intent.putExtra("id", mAdapter.getData().get(position).getId());
        switch (mAdapter.getData().get(position).getName()) {
            case "新生儿家庭访视记录":
                intent.setClass(this, XerDetailActivity.class);
                break;
            case "高血压患者随访记录":
                intent.setClass(this, GXYDetailActivity.class);
                break;
            case "2型糖尿病患者健康随访记录":
                intent.setClass(this, TnbDetailActivity.class);
                break;
            case "老年人中医药健康管理":
                intent.setClass(this, LaoNianRenDetailActivity.class);
                break;
            case "1到8月龄儿童健康检查记录":
                intent.setClass(this, Xer18DetailActivity.class);
                break;
            case "12到30月龄儿童健康检查记录":
                intent.setClass(this, Xer12DetailActivity.class);
                break;
            case "3到6岁儿童健康检查记录":
                intent.setClass(this, Xer36DetailActivity.class);
                break;
            case "儿童中医药健康管理服务记录":
                intent.setClass(this, ErTongZongYiDetailActivity.class);
                break;
            case "孕产妇一次产前检查记录":
                intent.setClass(this, ChanJianDetailActivity.class);
                break;
            case "孕产妇2到5次产前随访服务记录":
                intent.setClass(this, Chanjian25DetailActivity.class);
                break;
            case "孕产妇产后访视记录":
                intent.setClass(this, ChanHouDetailActivity.class);
                break;
            case "孕产妇产后42天健康检查记录":
                intent.setClass(this, ChanHou42DetailActivity.class);
                break;
            case "健康体检表":
                intent.setClass(this, HealthCheckDetailActivity.class);
                break;
        }
        startActivity(intent);
    }
}
